﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace BackUpAnyWhere
{
    static class Program
    {
        public enum UseCase
        {
            Start               = 0,
            UC1                 = 1,
            UC1_1               = 11,   /* 
                                         * this is not the first time of running the program, but 
                                         * for some reason the path is not available or the user
                                         * has ask to change the destination path, then moving back
                                         * to UC1. 
                                         * please note that after the user has selected the new
                                         * path, if it the same as what exist in the ini file, then
                                         * we change it back to UC2
                                         */
            UC2                 = 2,
            UC3                 = 3,
            UC3_1               = 31,
            UC3_2               = 32
        }

        public enum FileCounterType
        {
            FileCopy            = 1,
            FileSync            = 2,
            FileNotCopeid       = 3,
            FileCouldNotCopeid  = 4,
            FileExist           = 5,
            FileToDelete        = 6
        }

        public enum NotificationType
        {
            FileUpdate          = 1,
            FormClose           = 2
        }


        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            // Check if Autorun exist
            if (!Autorun.isAutorunExist())
                Autorun.AutorunCreate();

            // Start the project 
            if (!IniFileHandle.isIniExist())
            {
                /*
                 * If the ini file is not exist, this is the first time of
                 * running the program. at that case we need to create the 
                 * file, add the Server to the file and run the BackUpForm 
                 * with UC1.
                 */
                IniFileHandle.iniFileCreate();
                IniFileHandle.addNewServer();
                Application.Run(new BackUpForm((int)UseCase.UC1));
                if (BackUpForm.FileCounter[(int)BackUpAnyWhere.Program.FileCounterType.FileExist] > 1)
                    Application.Run(new PopUpForm(BackUpForm.currentRunningCase, BackUpForm.FileCounter, BackUpForm.FileToDeleteList));
            }
            else
            {
                /*
                 * If the file does exist, then we need to check whether the
                 * Server is exist in the file - if yes and the path which was
                 * use before is available, then this is UC2.
                 * if not, then this is UC1
                 */
                if (!IniFileHandle.isServerExist())
                {
                    IniFileHandle.addNewServer();
                    Application.Run(new BackUpForm((int)UseCase.UC1));
                    if (BackUpForm.FileCounter[(int)BackUpAnyWhere.Program.FileCounterType.FileExist] > 1)
                        Application.Run(new PopUpForm(BackUpForm.currentRunningCase, BackUpForm.FileCounter, BackUpForm.FileToDeleteList));
                }
                else
                {
                    /*
                     * At UC2, we need to validate the path of the previous backup
                     * is still available. if not then we should run as UC1. the only
                     * different should be is the message shown to the user.
                     */
                    if (!IniFileHandle.isPathAvailable())
                    {

                        Application.Run(new BackUpForm((int)UseCase.UC1_1));
                        if (BackUpForm.FileCounter[(int)BackUpAnyWhere.Program.FileCounterType.FileExist] > 1)
                            Application.Run(new PopUpForm(BackUpForm.currentRunningCase, BackUpForm.FileCounter, BackUpForm.FileToDeleteList));
                    }
                    else
                    {
                        if (!IniFileHandle.isAutomated())
                        {
                            Application.Run(new BackUpForm((int)UseCase.UC2));
                            if (BackUpForm.FileCounter[(int)BackUpAnyWhere.Program.FileCounterType.FileExist] > 1)
                                Application.Run(new PopUpForm(BackUpForm.currentRunningCase, BackUpForm.FileCounter, BackUpForm.FileToDeleteList));
                        }
                        else
                        {
                            Application.Run(new PopUpForm((int)UseCase.UC3, BackUpForm.FileCounter, BackUpForm.FileToDeleteList));
                            Application.Run(new BackUpForm(PopUpForm.currentRunningCase));
                            if (BackUpForm.FileCounter[(int)BackUpAnyWhere.Program.FileCounterType.FileExist] > 1)
                                Application.Run(new PopUpForm(BackUpForm.currentRunningCase, BackUpForm.FileCounter, BackUpForm.FileToDeleteList));
                        }
                    }
                }
            }
        }
    }
}
